<script type="text/javascript">
	function showReplyDiv(val) {		
		if(document.getElementById("replyDiv["+val+"]").style.display=="none") {
			document.getElementById("replyDiv["+val+"]").style.display="block";
		}
		else { 
			document.getElementById("replyDiv["+val+"]").style.display="none";
		}
	}
	
	function funSellerReply(bargain,replyTo) {		
		frm = document.frmProductOffer;
		document.getElementById("hidSellerReply").value=bargain;
		document.getElementById("hidReplyFor").value=replyTo;
		document.getElementById("hidType").value="add";
		frm.submit();		
	}
	
	function funAcceptBargain(bargain) {		
		//alert(bargain); return false;
		frm = document.frmProductOffer;
		document.getElementById("hidSellerReply").value=bargain;
		//document.getElementById("hidBarType").value=;
		document.getElementById("hidType").value="accept";
		frm.submit();		
	}
	
	function funDeleteBargain(bargain) {		
		frm = document.frmProductOffer;
		document.getElementById("hidSellerReply").value=bargain;
		document.getElementById("hidType").value="delete";
		frm.submit();		
	}
	
</script>
<style type="text/css">
	div.reply { padding:5px; border:0px solid #CCC; }	
	#grey-title { color:#999999; font-size:11px; }
	.line { border-bottom:1px solid #CCC; }
</style>
<?php
if(!empty($proId))
  $itemId=$proId;
  
    $sqlProOffers="SELECT * FROM tbl_bargain where item_id='$itemId' and status='offer'  and bargain_type=2";
	$rstProOffers=mysql_query($sqlProOffers) or die(mysql_error());
	$noProOffers=mysql_num_rows($rstProOffers);		
	if($noProOffers > 0) { 
		while($rowProOffers = mysql_fetch_array($rstProOffers)) {
			$pro_offers[]=$rowProOffers; 
		}			
	}	
	
	$off=0;
	if(is_array($pro_offers) > 0) { 
	
		echo "<table width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>";
		$i=0;
		foreach($pro_offers as $offer) { 
			if($offer['user_id']==$userId || $userId==$productOwner) {
					echo "<tr>
						<td>&nbsp;</td>
						<td height='30'>
							<strong>Offer</strong> <span id='grey-title'> Posted by ". getUser($offer['user_id'])."</span>
						</td>
						<td><strong>Price</strong></td>
						<td><strong>Commission</strong></td>
					</tr>
					<tr>
						<td width='3%'>&nbsp;</td>
						<td width='70%'>". $offer['comment']." </td>
						<td width='10%'>$". number_format($offer['offer_amount'],2)."</td>
						<td width='17%'>$". number_format($offer['offer_commision'],2)."</td>
					</tr>
					<tr>
						<td class='line'>&nbsp;</td>
						<td colspan='3' class='line'> ";				 
						
					   $sqlSelparent="SELECT * FROM tbl_bargain where parent_id='".$offer['bargain_id']."' and item_id='$itemId' and bargain_type=2";
					   $rstSelparent=mysql_query($sqlSelparent);
					   $pro_reply= mysql_fetch_array($rstSelparent);						
					   //display bargain options if bargain not accepted
					   if($offer['bargain_status']!='1') {
					   		if($userId==$productOwner) {							
							
							
									echo "<br><a href='javascript:void(0);' onClick='funAcceptBargain(".$offer['bargain_id'].");'>Accept</a>&nbsp;";						
									if($pro_reply=="") {																	
										echo "&nbsp;<a href='javascript:void(0);' onClick='showReplyDiv(".$i.");'>Reply</a>";
										?>						
										<div class="reply" id="replyDiv[<?php echo $i; ?>]" style="display:none;">
											<input type="hidden" name="ask_amount[<?php echo $i; ?>]" value="<?php echo $offer['asking_amount']; ?>">
											<input type="hidden" name="ask_commission[<?php echo $i; ?>]" value="<?php echo $offer['asking_commission']; ?>">							
											<table align="center" border="0" width="100%">
												<tr>
													<td width="2%">&nbsp;</td>
													<td width="25%">Bargaining price</td>
													<td width="73%"><input type="text" name="buyer_amount[<?php echo $i; ?>]" size="7" value="<?php echo $offer['asking_amount']; ?>"></td>
												</tr>
												<tr>
													<td>&nbsp;</td>
													<td>Bargaining commission</td>
													<td><input type="text" name="buyer_commission[<?php echo $i; ?>]" size="7" value="<?php echo $offer['asking_commission']; ?>"></td>
												</tr>
												<tr>
													<td>&nbsp;</td>
													<td>Comment</td>
													<td><textarea name="seller_comment[<?php echo $i; ?>]"></textarea></td>
												</tr>
												<tr>
													<td>&nbsp;</td>
													<td>&nbsp;</td>
													<td><input type="button" value="submit" onClick="funSellerReply('<?php echo $offer['bargain_id']; ?>','<?php echo $i; ?>');"></td>
												</tr>
												
											</table>
										</div>
								<?php 
									}
							}									
						} 
						else {
							echo "<span id='grey-title'> Ended. </span>";
						}					
						
					echo "<br><br></td></tr>";
					
					//display owners reply if exists
					
					if($pro_reply!="") {			
							echo '<tr bgcolor="#CCCCCC">
										<td>&nbsp;</td>
										<td height="30"><strong>Owners response</strong></td>
										<td><strong>Price</strong></td>
										<td><strong>Commission</strong></td>
								  </tr>
								  <tr bgcolor="#CCCCCC">
										<td width="3%">&nbsp;</td>
										<td width="70%">';						
											echo $pro_reply['comment']."<br>";
											
											//display bargain options if bargain not accepted
											if($offer['bargain_status']!='1') {											
												if($_SESSION['user_id_sess'] == $offer['user_id']) {
													echo "<a href='javascript:void(0);' onClick='funAcceptBargain(".$pro_reply['bargain_id'].");'>Accept</a>&nbsp;&nbsp;";									 
												}
												if($userId==$productOwner) {											
													echo "<a href='javascript:void(0);' onClick='funDeleteBargain(".$pro_reply['bargain_id'].");'>Delete</a>";
												}
											} else {
												echo "<span id='grey-title'> Ended. </span>";
											}										
											 
										echo '</td>
										<td width="10%">$'. number_format($pro_reply['offer_amount'],2).'</td>
										<td width="17%">$'. number_format($pro_reply['offer_commision'],2).'</td>
								  </tr>
								  <tr bgcolor="#CCCCCC"> <td colspan="4">&nbsp;</td></tr>';	
					 } 
					 $i++;
			} //else { $off=1; }	  
	    } 
		echo "</table>";		
	}	
	else { 
		$off=1; 	 
	}
	
if($off==1) { echo "<br><div id='suc-msg'>No Offers found.</div>"; }
?>